package dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dto.Player;

public class DataDisk implements Data {
	private static String FILE_PATH = "data/recode.dat";

	@SuppressWarnings("unchecked") // �ñ���������������
	@Override
	public List<Player> loadDate() {
		ObjectInputStream ois = null;
		List<Player> players = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(FILE_PATH));

			players = (List<Player>) ois.readObject();

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			try {
				ois.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return players;
	}

	@Override
	public void savaDate(Player player) {
		List<Player> players = new ArrayList();
		//�Ȼ�ȡ���е� �����б�
		players = this.loadDate();
		//������б���
		players.add(player);
		// �ж������Ƿ񳬹�5�� ������ ������ɾȥ�����
		if(players.size()>5){
			Collections.sort(players);
			for(int i=5;i<players.size();i++){
				players.remove(i); 
			}
		}
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(FILE_PATH));
			oos.writeObject(players);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				oos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	// public static void main(String[] args) {
	// List<Player> players =new ArrayList<Player>() ;
	// players.add(new Player("�°���", 1024));
	// players.add(new Player("��ʲ", 5024));
	// players.add(new Player("ϰ���", 10240));
	// players.add(new Player("���ʵ�", 2024));
	// players.add(new Player("������", 554));
	//
	// DataDisk dd =new DataDisk();
	// dd.savaDate(players);
	// System.out.println("�������");
	//
	// }
}
